package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.controllerApi.CategoryControllerApi;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;

@Slf4j
@RestController
@RequestMapping("/category")
public class CategoryController implements CategoryControllerApi {

    @Autowired
    private CategoryService categoryService;


    @PostMapping
    public R add(@RequestBody Category category){
        //先查 判断 再增

        categoryService.save(category);

        return R.success("添加成功");
    }


    @GetMapping("/page")
    public R findByPage(Integer page,Integer pageSize){
        Page<Category> pageInfo = new Page(page,pageSize);
        //添加一个条件，根据sort 字段进行升序的排序
        LambdaQueryWrapper<Category> wapper = new LambdaQueryWrapper<>();
        //添加排序条件即可
        wapper.orderByAsc(Category::getSort);
        Page page1 = categoryService.page(pageInfo,wapper);
        return R.success(page1);
    }



    @DeleteMapping
    public R deleteById(Long id){
        categoryService.delete(id);
        return R.success("删除成功");
    }

    @Override
    public R findSearch(Category category) {
        return null;
    }


    @PutMapping
    public R update(@RequestBody Category category){
        //根据id修改数据
        categoryService.updateById(category);

        return R.success("修改成功..");
    }

    //完成此接口文档的定义
    @GetMapping("/list")
    public R list(Integer type){
        //1根据分类的类型查询所有的分类数据
        LambdaQueryWrapper<Category> wapper = new LambdaQueryWrapper<>();
        if(type != null){
            wapper.eq(Category::getType,type);
        }
        List<Category> list = categoryService.list(wapper);
        return R.success(list);
    }

    @PostMapping("/test")
    public R test(@RequestBody Category category){


        return R.success("成功");
    }

}
